<?php
namespace app\admin\controller;
use think\Controller;
use think\Controllers;
use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Member extends Controllers{

    public function index(){
        
    $pindex    = max(1, intval($_REQUEST['page']));
    $psize     = 20;

    $wheres = " AND a.uniacid= 2";

    if(!empty($_REQUEST['id'])){

        $wheres.=" and a.uid= '{$_REQUEST['id']}'  ";
        $this->assign("id",$_REQUEST['id']);

        
    }
    if(!empty($_REQUEST['phone'])){

        $wheres.=" and a.mobile='{$_REQUEST['phone']}' ";
        $this->assign("phone",$_REQUEST['phone']);



    }
    if(!empty($_REQUEST['yewuy'])){

        $wheres.=" and b.yewuy= '{$_REQUEST['yewuy']}' ";
        $this->assign("yewuy",$_REQUEST['yewuy']);


    }

    if(!empty($_REQUEST['nickname'])){

        $wheres.=" and a.nickname LIKE '%".$_REQUEST['nickname']."%'";
        $this->assign("nickname",$_REQUEST['nickname']);
        
    }

    if(!empty($_REQUEST['level'])&&$_REQUEST['level']!=0){

        $wheres2=" and b.level=".intval($_REQUEST['level']);
            $this->assign("level",$_REQUEST['level']);
        

        $sql ="SELECT a.uid,a.nickname,b.yewuy,a.mobile as phone,a.avatar,a.credit2,a.createtime,b.level,b.name,b.weixin,b.mobile,b.idcard FROM ims_mc_members as a inner join  ims_xuan_zy_person as b on b.uid=a.uid {$wheres2} WHERE 1 {$wheres} order by a.uid desc";
        $sql2 ="SELECT count(*) as count FROM ims_mc_members as a inner join ims_xuan_zy_person as b on b.uid=a.uid {$wheres2} WHERE 1 {$wheres} order by a.uid desc";

    }else{

        $sql ="SELECT a.uid,a.nickname,b.yewuy,a.avatar,a.mobile as phone,a.credit2,a.createtime,b.level,b.name,b.weixin,b.mobile,b.idcard FROM ims_mc_members as a left join ims_xuan_zy_person as b on b.uid=a.uid   WHERE 1 {$wheres} order by a.uid desc";
        $sql2 ="SELECT count(*) as count FROM  ims_mc_members as  a left join ims_xuan_zy_person as b on b.uid=a.uid WHERE 1 {$wheres} order by a.uid desc";

    }
    $limit = ($pindex-1)*$psize;
    $sql .= "  limit {$limit},{$psize}";
    $list = DB::query($sql);
    foreach ($list as $key => &$value) {
        if($value['level'] == 1){
            $value['level'] = "专员";
        } else if ($value['level'] == 2) {
            $value['level'] = "经理";
        } else if ($value['level'] == 3) {
            $value['level'] = "银行家";
        } else {
            $value['level'] = "普通用户";
        }
        if($value['yewuy'] == 1){
            $value['yewuy'] = "是";
        } else{
            $value['yewuy'] = "否";
        }
        if(empty($value['name'])){
            $value['name'] ="";
        }  
        if(empty($value['weixin'])){
            $value['weixin'] ="";
        }
        if(empty($value['idcard'])){
            $value['idcard'] ="";
        }
        
        $value['createtime'] = date("Y-m-d H:i:s",$value['createtime']);
    }
    $count = DB::query($sql2)[0]['count'];
    if(request()->isajax()){

        echo json_encode($list);die;
    }
    $this->assign("list",$list);
    $this->assign("count",$count);
        return $this->fetch("admin/member");

    }
    public function add(){
        $id =$_REQUEST['id'];
        $item = DB::query("SELECT * FROM ims_mc_members WHERE uid={$id} AND uniacid=2")[0];
        $item['createtime'] = date("Y-m-d H:i:s",$item['createtime']);
    $item['info']=DB::query("SELECT * FROM  ims_xuan_zy_person where uid={$id} AND uniacid=2")[0];
    $item['member']=DB::query("SELECT * FROM ims_mc_mapping_fans where  uid={$id}")[0];
    //获取上级
    $item['upuid']=DB::query("SELECT * FROM ims_xuan_zy_userup where  uid={$id}")[0];
    if(empty($item['upuid'])){
        $arr = array('uid'=>$item['uid'],'uniacid'=>2,'create_time'=>date('Y-m-d H:i:s',time()));
        DB::table('ims_xuan_zy_userup')->insert($arr);
        $item['upuid']=  DB::query("SELECT * FROM ims_xuan_zy_userup where  uid={$item['uid']}")[0];
    }

      $sql ="select SUM(money) as money from ims_xuan_zy_tixian where uid={$id} and uniacid=2 and (status=1 or status=4 or status=5)";
      $money = DB::query($sql)[0]['money'];
      $this->assign("user",$item);
      $this->assign("id",$id);
      $this->assign("money",$money);
      return $this->fetch("admin/member_add");
    }
    public function upload(){
        $id =$_REQUEST['id'];
        DB::table("ims_mc_members")->where("uid","{$id}")->setField(array("mobile"=>$_REQUEST['info']['mobile']));
        DB::table("ims_xuan_zy_person")->where("uid","{$id}")->setField($_REQUEST['info']);
        DB::table("ims_xuan_zy_userup")->where("uid","{$id}")->setField($_REQUEST['upuid']);
    $this->success("修改成功","admin/member/index");

    }
}